<?php
Class Userecoupon extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'userecoupon';
        $this->load->model('Muserecoupon');
        $this->checkAdmin($this->session->userdata('admin_id'),$this->session->userdata('restaurant_id'));
        
    }
    
    public function index()
    {
       
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 15;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Muserecoupon->getEcouponUserDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));
        $this->data['restaurant'] = $this->Muserecoupon->restaurantName();
        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        /*
        if($this->form_validation->run() === TRUE)
        {
            if($this->_per->del != 4)
            {
                $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
                exit();
            }
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Muserecoupon->deleteEcouponUserDB($idArray);
            if(intval($run) == 200)
            {
               // die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");window.location="'.$this->data['baseurl'].'manage/ecoupon/index";</script>');
            }
        }
        */
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
    
    public function adduserecoupon()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('active', 'Trạng thái sử dụng', 'required');
		
		$this->form_validation->set_rules('code_coupon', 'Mã E-coupon', 'required');
		$this->form_validation->set_rules('restaurant_id', 'Nhà hàng', 'required');
		$this->form_validation->set_rules('log_work_car', 'Ca làm việc', 'required');
		$this->form_validation->set_rules('log_bills', 'Số bills', 'required');
		$this->form_validation->set_rules('log_customers', 'Số khách', 'required');
		$this->form_validation->set_rules('log_revenue', 'Doanh thu', 'required');
		$this->form_validation->set_rules('log_customer', 'Tên khách hàng', 'required');
		$this->form_validation->set_rules('log_mobile', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('log_gift', 'Nội dung voucher', 'required');
		
		
        $this->data['success'] = '';
        if($this->form_validation->run() === TRUE)
        {
			$restaurant_id = $this->getParamInt('restaurant_id');
			$log_work_car = $this->getParamString('log_work_car');
			$log_bills = $this->getParamString('log_bills');
			$log_customers = $this->getParamString('log_customers');
			$log_revenue = $this->getParamString('log_revenue');
			$log_customer = $this->getParamString('log_customer');
			$log_mobile = $this->getParamString('log_mobile');
			$log_gift = $this->getParamString('log_gift');
			
			$active = $this->getParamInt('active');
			$id = $this->getParamInt('id');
			$code_coupon = $this->getParamString('code_coupon'); // code_coupon
            $run = $this->Muserecoupon->addUserEcouponDB($id,$code_coupon,$active,$restaurant_id,$log_work_car,$log_bills,$log_customers,$log_revenue,$log_customer,$log_mobile,$log_gift);
            if(intval($run) == 200)
            {
                $this->data['success'] = 200;
				$this->data['code_coupon'] = $code_coupon;
            }else if(intval($run) == 201)
            {
                $this->data['success'] = 200;
				$this->data['code_coupon'] = $code_coupon;
            }
        }
        
        $this->data['restaurant'] = $this->Muserecoupon->restaurantName();
        $this->data['listCouponActive'] = $this->Muserecoupon->getEcouponInRestaurant();
        
        $this->data['action'] = 'adduserecoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    public function searchuserecoupon()
    {
        $code_coupon = $this->getParamString('code');
        $this->data['data'] = $this->Muserecoupon->getForm($code_coupon);
		$this->data['listRestaurant'] = $this->Muserecoupon->listRestaurant($this->session->userdata('restaurant_id'),$code_coupon);
        $this->load->view('template/userecoupon/searchuserecoupon', $this->data);
    }
    
    public function deleteuserecoupon()
    {
        $idArray = $this->getParamUri(4,1);
        $run = $this->Mecoupon->deleteEcouponUserDB($idArray);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/userecoupon/user-ecoupon";</script>');
        }
    }
    
	public function ajaxexport1ecoupon()
    {
		$code_coupon = $this->getParamString('code_coupon'); //die(''.$code_coupon);
        $this->data['data'] = $this->Muserecoupon->ajaxExport1Ecoupon($code_coupon); //echo '<pre>';die(print_r($this->data['data']));
        $this->data['action'] = 'ajaxexport1ecoupon';
        $this->load->view('template/userecoupon/ajaxexport1ecoupon', $this->data);
    }
}